﻿/* 
 *========================================================================
 * 描述信息 
 *  
 * 作者：w       
 * 时间：2021/4/29 16:49:36 
 * 文件名：StartCapture 
 * CLRVersion：4.0.30319.42000
 * 版本：V1.0.0 
 * 
 * 修改者：           
 * 时间：               
 * 修改说明： 
 *======================================================================== 
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpPcap;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    public class StartCapture
    {
        private ICaptureDevice _device;
        public StartCapture(ICaptureDevice device)
        {
            this._device = device;
        }

        public void Start() {
            Task.Factory.StartNew(() => {
                // 定义“包到达”事件
                _device.OnPacketArrival += new SharpPcap.PacketArrivalEventHandler(NetCapture.Device_OnPacketArrival);
                // 打开连接
                int readTimeoutMilliseconds = 1000;
                _device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
                // 设置仅获取目标端口为1234的tcp包
                //string filter = "tcp dst port 1234";
                //device.Filter = filter;
                // 开始捕获包
                _device.Capture();
            });
        }
    }
}
